public with sharing class AT_Send2Intacct
{
    public Account begObj {get; set;}   
    public Account endObj {get; set;}

    public Account begObj2 {get; set;}  
    public Account endObj2 {get; set;}


    public List<TransactionExtend> transactions {get; set;}
    public List<TransactionExtend> transactions2 {get; set;}
    public boolean isLimitQuery {get; set;}
    public boolean isWasSent {get; set;}
    public boolean isWasSent2 {get; set;}
    public String selItemID {get; set;}
    public String strStatusMsg {get; set;}
    
    public boolean isShowResend {get; set;}
    
    public static boolean isTest = false;
    
    public AT_Debug_Log__c begubObj {get; set;}
    
    public AT_Send2Intacct()
    {
        strStatusMsg = '';
        begObj = new Account();
        endObj = new Account();
        isWasSent = false;
        isShowResend = true;

        begObj2 = new Account();
        endObj2 = new Account();
        isWasSent2 = false;

        
        begubObj = new AT_Debug_Log__c();
    }

    public Boolean getIsTransactionsExists()
    {
        if(transactions!=null && transactions.size()>0) return true;

        return false;
    }
    
    public Boolean getIsTransactionsNonExists()
    {
        if(transactions!=null && transactions.size()>0) return false;

        return true;
    }

    public Boolean getIsTransactionsExists2()
    {
        if(transactions2!=null && transactions2.size()>0) return true;

        return false;
    }
    
    public Boolean getIsTransactionsNonExists2()
    {
        if(transactions2!=null && transactions2.size()>0) return false;

        return true;
    }

    
    public Boolean getIsSelect()
    {
        if(transactions!=null) return true;

        return false;
    }
    
    public Boolean getIsSelect2()
    {
        if(transactions2!=null) return true;

        return false;
    }


    public PageReference runBatch()
    {
        integer batchSize = 100;
        AT_ProcessAllOpportunities b = new AT_ProcessAllOpportunities(); 
        database.executebatch(b, batchSize);
        strStatusMsg = 'Process of create Transaction from Opportunity has been started.';      
        
        return null;
    }
    
    public PageReference runBatch2()
    {
        integer batchSize = 200;
        AT_ProcessAllDLogs b = new AT_ProcessAllDLogs(); 
        database.executebatch(b, batchSize);
        strStatusMsg = 'Process of fix Debug Logs has been started.';       
                    
        return null;
    }

    public PageReference runBatch3()
    {
        integer batchSize = 200;
        AT_ProcessAllDLogsType b = new AT_ProcessAllDLogsType(); 
        database.executebatch(b, batchSize);
        strStatusMsg = 'Process of fill Debug Logs Transaction Type has been started.';     
                    
        return null;
    }

    public PageReference Send2()
    {
        if(begubObj!=null && begubObj.Transaction_Header__c!=null)
        {
            system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>THID: '+begubObj.Transaction_Header__c);
            AT_sendIntacctCallOut.sendToInTacctReq(begubObj.Transaction_Header__c);
        }
        return null;
    }

    public PageReference Send()
    {
        if(selItemID!=null && selItemID!='')
        {
            system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>THID: '+selItemID);
            AT_sendIntacctCallOut.sendToInTacctReq((ID)selItemID);
        }
        
        searchTr();
        return null;
    }

    public PageReference Resend2()
    {
        isShowResend = false;
        if(selItemID!=null && selItemID!='')
        {
            for(AT_Debug_Log__c item:[select id from AT_Debug_Log__c where Status__c='Failure' and Transaction_Type__c='Sales Invoice' and Transaction_Header__c=:selItemID limit 1])
            {
                String trKey = '';
                for(AT_Debug_Log__c item2Key:[select id,Transaction_Key__c from AT_Debug_Log__c where Status__c='Success' and Transaction_Type__c='Sales Order' and Transaction_Header__c=:selItemID limit 1])
                {
                    trKey = item2Key.Transaction_Key__c;
                }
                if(trKey!='')
                {
                    //system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>THID: '+selItemID);
                    AT_sendIntacctCallOut.sendToInTacctInvoice(selItemID,item.id,trKey);
                }
            }
        }
        
        return null;
    }


    // Miss Transactions
    public PageReference searchTr()
    {
        Integer SQLResLimit = 1000;
        if(AT_Send2Intacct.isTest) SQLResLimit = 10;
        
        isWasSent = false;
        transactions = new List<TransactionExtend>();
        Date startDate = begObj.PersonBirthdate;
        Date endDate = endObj.PersonBirthdate;
        if(startDate==null) startDate = date.newInstance(2011, 1, 1);
        if(endDate==null) endDate = date.today();
        isLimitQuery = false;
        
        Map<ID,Transaction_Header__c> trResult = new Map<ID, Transaction_Header__c>([select Id,CreatedDate,Name,Account__c from Transaction_Header__c where CreatedDate>=:startDate and CreatedDate<=:endDate and isFromOpportunity__c!=true limit :SQLResLimit]);
        if(trResult.size()>=SQLResLimit) isLimitQuery = true;
        
        if(trResult.size()>0)
        {
            Map<ID,ID> mapTHIdInDebug = new Map<ID,ID>();
            
            for(AT_Debug_Log__c item:[select Id,Transaction_Header__c from AT_Debug_Log__c where Transaction_Header__c in :trResult.keySet() limit :SQLResLimit])
            {
                mapTHIdInDebug.put(item.Transaction_Header__c,item.Transaction_Header__c);
            }
            
            for(Transaction_Header__c item:trResult.values())
            {
                if(!mapTHIdInDebug.containsKey(item.ID) && transactions.size()<300){ transactions.add(new TransactionExtend(item));}
            }
        }
        

        return null;
    }

    // Transactions w/o Invoice
    public PageReference searchTr2()
    {
        Integer SQLResLimit = 1000;
        if(AT_Send2Intacct.isTest) SQLResLimit = 10;
        
        isWasSent2 = false;
        transactions2 = new List<TransactionExtend>();
        Date startDate = begObj2.PersonBirthdate;
        Date endDate = endObj2.PersonBirthdate;
        if(startDate==null) startDate = date.newInstance(2011, 1, 1);
        if(endDate==null) endDate = date.today();
        isLimitQuery = false;
        
        //Map<ID,Transaction_Header__c> trResult = new Map<ID, Transaction_Header__c>([select Id,CreatedDate,Name,Account__c from Transaction_Header__c where CreatedDate>=:startDate and CreatedDate<=:endDate and isFromOpportunity__c!=true limit 10000]);
        //if(trResult.size()>=10000) isLimitQuery = true;
        
        List<AT_Debug_Log__c> dLogList = [select Id,Transaction_Header__c,Transaction_Date__c,Status__c,Transaction_Type__c from AT_Debug_Log__c where Status__c='Failure' and Transaction_Type__c='Sales Invoice' and Transaction_Date__c>=:startDate and Transaction_Date__c<=:endDate limit :SQLResLimit];
        if(dLogList.size()>=SQLResLimit) isLimitQuery = true;
        
        Set<ID> trIDs = new Set<ID>();
        
        if(dLogList.size()>0)
        {
            for(AT_Debug_Log__c item:dLogList){ trIDs.add(item.Transaction_Header__c);}
            
            for(Transaction_Header__c item:[select Id,CreatedDate,Name,Account__c from Transaction_Header__c where id in :trIDs limit :SQLResLimit])
            {
                transactions2.add(new TransactionExtend(item));
            }
        }
        

        return null;
    }
    
    public with sharing class TransactionExtend
    {
        public Boolean Checked{ get; set; }
        public Transaction_Header__c Item { get; set;}
 
        public TransactionExtend() {
            Item = new Transaction_Header__c();
            Checked = false;
        }
 
        public TransactionExtend(Transaction_Header__c inItem) {
            Item = inItem;
            Checked = false;
        }
    }   
}